package com.cmbt.controller;

import com.cmbt.bean.Page;
import com.cmbt.bean.Role;
import com.cmbt.service.api.RoleService;
import com.cmbt.service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class RoleController extends BaseController{
    RoleService roleService = new RoleServiceImpl();
    //分页展示
    public void rolePageServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //获取当前页数，首次进入时currentPage为1，点击超链接时获取currentPage页数
        int currentPage = request.getParameter("currentPage") == null ? 1 : Integer.parseInt(request.getParameter("currentPage"));


        // 2、计算从哪里开始
        //通过page实现类获得总页数，这里的16为每页数目
        int pageSize = 16;
        int totalPage = roleService.getTotalPage(pageSize);



        // 3、前一页页数、后一页变量值
        int prePage = currentPage - 1 > 0 ? currentPage - 1 : currentPage;
        int nextPage = currentPage + 1 < totalPage ? currentPage + 1 : totalPage;


        //使用request.setAttribute方法便于页面中使用el语法
        request.setAttribute("totalPage", totalPage);
        request.setAttribute("prePage", prePage);
        request.setAttribute("nextPage", nextPage);
        request.setAttribute("isFirst",false);
        //获得当前页的数据
        Page page = new Page();
        page.setCurrentPage(currentPage);
        page.setPageSize(pageSize);

        List<Role> roleList = roleService.queryUserByPage(page);
        request.setAttribute("roleList", roleList);
        request.setAttribute("currentPage", currentPage);
        request.getRequestDispatcher("role.jsp").forward(request, response);
    }
}
